package com.taptech.doufu.ui.viewholder.personalcenter;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.bean.MineAbstractBean;
import com.taptech.doufu.bean.MineNovelBean;
import com.taptech.doufu.bean.MineNovelReviewBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.ui.view.drawcircle.BaseRecyclerViewViewHolder;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.ui.view.personalcenter.NewMyCollectView;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.StartActivityUtil;
import com.taptech.doufu.util.UIUtil;

/**
 * Created by lsy on 2015/9/29.
 */
public class SweepRecyclerViewHolder extends BaseRecyclerViewViewHolder implements View.OnClickListener, View.OnLongClickListener
{

    Context mContext;
    // 总数据
    MineNovelReviewBean bean;
    MineNovelBean novelBean;

    // 进入详情的点击控件
    protected RelativeLayout itemSweepClickBtn;

    public TextView mTitleLevel;
    public TextView sweepTitle;
    public TextView sweepDes;
    public TextView sweepReadTimes;
    public TextView mUserName;
    public TextView mAuthorName;
    public TextView commentCount;
    public ImageView sweepReadTimeImg;
    public ImageView mUserImg;
    public ImageView mUserMedal;
    protected Bitmap defaultBitmap = null;
    protected TextView mDeleteBtn;
    protected TextView mCreateTime;

    private int mPositon;

    public SweepRecyclerViewHolder(Context context, View convertView)
    {
        super(context, convertView);
        mContext = context;
        itemSweepClickBtn = (RelativeLayout) convertView.findViewById(R.id.item_sweep_click_btn);
        sweepTitle = (TextView) convertView.findViewById(R.id.home_item_sweep_title);
        sweepDes = (TextView) convertView.findViewById(R.id.home_item_sweep_des);
        sweepReadTimeImg = (ImageView) convertView.findViewById(R.id.home_item_sweep_read_time_icon);
        sweepReadTimes = (TextView) convertView.findViewById(R.id.home_item_sweep_read_times);
        commentCount = (TextView) convertView.findViewById(R.id.home_item_comment_count);
        mUserImg = (ImageView) convertView.findViewById(R.id.home_item_sweep_userimg);
        mUserName = (TextView) convertView.findViewById(R.id.home_item_sweep_username);
        mAuthorName = (TextView) convertView.findViewById(R.id.home_item_sweep_author);
        mTitleLevel = (TextView) convertView.findViewById(R.id.home_item_sweep_title_level);
        mUserMedal = (ImageView) convertView.findViewById(R.id.home_item_sweep_userimg_medal);
        mDeleteBtn = (TextView) convertView.findViewById(R.id.delete_item_btn);
        mCreateTime = (TextView) convertView.findViewById(R.id.home_item_sweep_create_time);
        rankImageView = (ImageView) itemView.findViewById(R.id.home_rank_position);
    }

    @Override
    public void setViewHolderChildViewContent(MineAbstractBean object, int position)
    {
        bean = (MineNovelReviewBean) object;

        if (bean == null)
        {
            return;
        }
        novelBean = bean.getNovel();
        if (novelBean == null)
        {
            return;
        }
        mPositon = position;
        itemSweepClickBtn.setOnClickListener(this);
        itemSweepClickBtn.setOnLongClickListener(this);
        if (mDeleteBtn != null)
        {
            mDeleteBtn.setOnClickListener(this);
        }
        mUserImg.setOnClickListener(this);
        // 用户头像
        // 默认头像
        if (bean.getUser() != null) {
            GlideUtil.displayCircleImage(mUserImg, bean.getUser().getIcon());
        }else{
            GlideUtil.displayCircleImage(mContext,mUserImg, R.drawable.default_user_portrait);
        }
        // 文评星级显示
        if (bean.getStar() != null)
        {
            String level = UIUtil.showStarLevel(bean.getStar(), mTitleLevel);
            mTitleLevel.setText(level);
        }
        // 文评标题
        if (novelBean.getTitle() != null)
        {
            sweepTitle.setText(novelBean.getTitle());
        }
        // 用户名字
        if (bean.getUser() != null)
        {
            mUserName.setText(bean.getUser().getName());
        }
        // 作者名字
        if (novelBean.getAuthor() != null)
        {
            mAuthorName.setText("作者：" + novelBean.getAuthor());
        }
        if (bean.getReview() != null)
        {
            sweepDes.setText(bean.getReview());
        }
        // 被浏览数
        if (bean.getRead_num() != null)
        {
            sweepReadTimes.setText(bean.getRead_num());
        }
        // 被评论数
        if (bean.getComment_times() != null)
        {
            commentCount.setText(bean.getComment_times());
        }
        if (bean.getUser() != null && bean.getUser().getMedal() != null)
        {
            DiaobaoUtil.setMedalImgView(bean.getUser().getMedal(), mUserMedal);
        }

        if (bean.getSpecialType() == SPECIAL_TYPE_COLLECT)
        {
            mDeleteBtn.setVisibility(View.VISIBLE);
            mDeleteBtn.setText("取消收藏");

        } else if (bean.getSpecialType() == SPECIAL_TYPE_PERSONAL_DELETE && bean.getUid().equals(AccountService.getInstance().getUserUid()))
        {
            mDeleteBtn.setVisibility(View.VISIBLE);
            mDeleteBtn.setText("删除");

        } else
        {
            mDeleteBtn.setVisibility(View.GONE);
        }

        if (bean != null && bean.getAdd_time() != null)
        {
            mCreateTime.setText(DiaobaoUtil.seconds2TimeString(Long.parseLong(bean.getAdd_time())));
        }
        if (bean.isRank_flag())
        {
            setRankImageView(position + 1);
        } else if (rankImageView != null)
        {
            rankImageView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.item_sweep_click_btn:
                SimpleWeexActivity.startActivity(mContext,"vue/articalComment/TFArticalComment.js?novelId="+novelBean.getId());
                break;
            case R.id.home_item_sweep_userimg:
                String userId = "";
                if (bean != null && bean.getUser() != null) {
                    userId = bean.getUser().getUserId();
                }
                StartActivityUtil.enterOtherPersonalCenter(mContext, userId);
                break;
            case R.id.delete_item_btn:
                if (bean.getSpecialType() == SPECIAL_TYPE_COLLECT)
                {
                    Message message = new Message();
                    message.what = NewMyCollectView.DEL_COLLECT_ITEM_COMMAND;
                    Bundle bundle = new Bundle();
                    bundle.putString("id", bean.getId());
                    bundle.putInt("position", mPositon);
                    bundle.putString(Constant.OBJECT_TYPE, bean.getObject_type());
                    message.setData(bundle);
                    if (mHandler != null)
                    {
                        mHandler.sendMessage(message);
                    }
                } else if (bean.getSpecialType() == SPECIAL_TYPE_PERSONAL_DELETE && bean.getUid().equals(AccountService.getInstance().getUserUid()))
                {
                    Message message = new Message();
                    message.what = BaseAdapterViewHolder.DELETE_NOVEL_REVIEW_ITEM;
                    Bundle bundle = new Bundle();
                    bundle.putString("id", bean.getId());
                    bundle.putInt("position", mPositon);
                    bundle.putString(Constant.OBJECT_TYPE, bean.getNovel().getObject_type());
                    message.setData(bundle);
                    if (mHandler != null)
                    {
                        mHandler.sendMessage(message);
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onLongClick(View view)
    {
        switch (view.getId())
        {
            case R.id.item_sweep_click_btn:
                if (bean.getSpecialType() == SPECIAL_TYPE_COLLECT)
                {
                    Message message = new Message();
                    message.what = NewMyCollectView.DEL_COLLECT_ITEM_COMMAND;
                    Bundle bundle = new Bundle();
                    bundle.putString("id", bean.getId());
                    bundle.putInt("position", mPositon);
                    bundle.putString(Constant.OBJECT_TYPE, bean.getObject_type());
                    message.setData(bundle);
                    if (mHandler != null)
                    {
                        mHandler.sendMessage(message);
                    }
                } else if (bean.getSpecialType() == SPECIAL_TYPE_PERSONAL_DELETE && bean.getUid().equals(AccountService.getInstance().getUserUid()))
                {
                    Message message = new Message();
                    message.what = BaseAdapterViewHolder.DELETE_NOVEL_REVIEW_ITEM;
                    Bundle bundle = new Bundle();
                    bundle.putString("id", bean.getId());
                    bundle.putInt("position", mPositon);
                    bundle.putString(Constant.OBJECT_TYPE, bean.getNovel().getObject_type());
                    message.setData(bundle);
                    if (mHandler != null)
                    {
                        mHandler.sendMessage(message);
                    }
                }
                break;
        }
        return false;
    }
}
